class updown:
    def __init__(self):
        self.preb=None
        self.timekey=None
        self.his=[]
        self.fx=None
    def oncalc(self,b,timekey=None):
        """
        Args:
            b:曲线向上1，向下-1,拐点  2，-2

        Returns:

        """
        if timekey   is not None:
            if timekey == self.timekey:
                self.his.pop()
                self.preb,self.fx = self.his[-1]
        if b is not None:
            if self.preb is not None:
                if self.fx is None:
                    if self.preb<b:
                        self.fx=1
                    else:
                        self.fx=-1
                elif self.fx>=1:
                    if b<self.preb:
                        self.fx=-2
                    else:
                        self.fx = 1
                elif self.fx<=-1:
                    if b>self.preb:
                        self.fx=2
                    else:
                        self.fx=-1
            self.preb=b
            if timekey   is not None:
                self.timekey = timekey
                self.his.append((self.preb,self.fx))
                while len(self.his) > 2:
                    self.his.pop(0)
        else:
            self.fx=None
        return self.fx
class updown_stat:
    def __init__(self,stat=[1,-1],focus=[None,0,0]):
        """

        Args:
            stat: 涨跌2状态；涨跌拐点4状态  （后续可以考虑涨跌平3状态）
            focus: 是否注意 聚焦单个方向；及容忍误差  百分比；
        """
        self.preb=None
        self.code=focus[2]
        self.timekey=None
        self.his=[]
        self.stat=stat
        self.focus=focus
        if len(stat)==2:
            self.oncalc = self.oncalc2
        else:
            self.oncalc=self.oncalc4
        self.fx=None
    def oncalc4(self,b,timekey=None):
        """
        Args:
            b:曲线向上1，向下-1,拐点  2，-2

        Returns:

        """
        if timekey   is not None:
            if timekey == self.timekey:
                self.his.pop()
                self.preb,self.fx = self.his[-1]
        if b is not None:
            if self.preb is not None:
                if self.fx is None:
                    if self.preb<b:
                        self.fx=1
                    else:
                        self.fx=-1
                elif self.fx>=1:
                    if b<self.preb:
                        self.fx=-2
                    else:
                        self.fx = 1
                elif self.fx<=-1:
                    if b>self.preb:
                        self.fx=2
                    else:
                        self.fx=-1
            self.preb=b
            if timekey   is not None:
                self.timekey = timekey
                self.his.append((self.preb,self.fx))
                while len(self.his) > 2:
                    self.his.pop(0)
        else:
            self.fx=None
        return self.fx
    def oncalc2(self,b,timekey=None):
        """
        Args:
            b:曲线向上1，向下-1
        Returns:
        """
        if b is not None:
            if timekey   is not None:
                if timekey == self.timekey:
                    self.his.pop()
                    self.preb,self.code = self.his[-1]
            if self.preb is not None:
                if self.code==self.stat[0]:
                    if self.preb>b:
                        self.code=self.stat[1]
                elif self.code==self.stat[1]:
                    if self.preb<b:
                        self.code=self.stat[0]
                else:
                    if self.code==0:
                        if self.preb<b:
                            self.code = self.stat[0]
                    else:
                        if self.preb>b:
                            self.code = self.stat[1]
            self.preb=b
            if timekey   is not None:
                self.timekey = timekey
                self.his.append((self.preb,self.code))
                while len(self.his) > 2:
                    self.his.pop(0)
        return self.code
if __name__ == '__main__':
    from jili.core import load, save
    from research.calcor.calcors_graph import graph_calcor
    k1m = load(r"D:\data\k1m_tq\TA109\202105_k1m.pkl")
    updown0=updown()
    cc = [{'calc_cmd': 'ta', 'cmd': 'EMA', 'out': ['ema30'], 'input': {'price': 'close'}, 'arg': {'timeperiod': 30},
           'batch': 30},
          {'calc_cmd': 'ta', 'cmd': 'EMA', 'out': ['ema10'], 'input': {'price': 'close'}, 'arg': {'timeperiod': 10},
           'batch': 10},
          {'calc_cmd': 'function', 'function': updown0.oncalc, 'out': ['updown0'], 'input': ["close"]}]
    c = graph_calcor(cc)
    for d, v in k1m.items():
        for t in v:
            t = c.onbar(t)
    print("end")